package light.head.controller.oa;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ContractTypeDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ContractType;

@Controller
@RequestMapping(value = Url.C_CONTRACTTYPE)
public class ContractTypeController extends SupporterController{
	
	private ContractTypeDAO contractTypeDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.CONTRACTTYPE_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.OC);
		map.put(Parm.OC, cu_organCode(request).toString());
		if(contractTypeDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		
		return new ModelAndView(Url.CONTRACTTYPE_LIST);
	}


	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)) {
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.CONTRACTTYPE_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)) {
			ContractType cType = contractTypeDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.CONTRACTTYPE, cType);
		}
		return new ModelAndView(Url.CONTRACTTYPE_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(ContractType cType, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(null == cType.getCode()) {
			cType.setCt(new Date());
			cType.setCu(cu_code(request));
			cType.setDeleted(false);
			cType.setOc(cu_organCode(request));
			result = contractTypeDAO.add(cType);
		}else {
			result = contractTypeDAO.update(cType);
		}
		
		if(RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		}else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		ContractType cType = contractTypeDAO.get(Integer.parseInt(code));
		cType.setDeleted(true);
		if (contractTypeDAO.update(cType) == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}

	@Autowired
	public void setContractTypeDAO(ContractTypeDAO contractTypeDAO) {
		this.contractTypeDAO = contractTypeDAO;
	}
	
	
}
